package de.schlinke_marius.fragments;

import android.app.Activity;
import android.database.Cursor;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import de.schlinke_marius.R;
import de.schlinke_marius.activities.MainTabActivity;
import de.schlinke_marius.application.MSComputerApplication;
import de.schlinke_marius.data.AppContainer;
import de.schlinke_marius.data.ConsultingItem;
import de.schlinke_marius.data.FragmentConstants;
import de.schlinke_marius.database.MSCDatabase;
import de.schlinke_marius.dialogs.ConsultingDialog;

public class ConsultingFragment extends Fragment implements OnClickListener, FragmentConstants {

	/*
	 * UI elements
	 */
	private TextView tvQuestion = null;
	private RadioGroup rgAnswers = null;
	private RadioButton rbAnswerThree = null;
	private RadioButton rbAnswerOne = null;
	private RadioButton rbAnswerTwo = null;
	private ImageButton ibPreviousQuestion = null;
	private ImageButton ibNextQuestion = null;
	private ProgressBar pbProgressQuestions = null;
	private Button btAnalyseQuestions = null;
	private Button btRestartQuestions = null;

	/*
	 * Variables
	 */
	private static final String TAG = ConsultingFragment.class.getSimpleName();
	private MainTabActivity activity = null;
	private MSCDatabase db = null;
	private int currentQuestion = 1;
	private Cursor cursorQuestion = null;
	private Cursor cursorAnswers = null;
	private Cursor countQuestions = null;
	private AppContainer globalData = null;
	private int questionCount = 0;

	/*
	 * Constants
	 */
	private static final int NEXT_QUESTION = 1;
	private static final int PREVIOUS_QUESTION = 2;

	@Override
	public void onAttach(Activity activity) {
		super.onAttach(activity);
		intializeReferences(activity);
	}

	@Override
	public void onSaveInstanceState(Bundle outState) {
		super.onSaveInstanceState(outState);
		if (outState.isEmpty()) {
			outState.putBoolean("bug:fix", true);
		}
	}

	
	private void intializeReferences(Activity activity) {
		db = new MSCDatabase(activity);
		this.activity = (MainTabActivity) activity;
		MSComputerApplication application = (MSComputerApplication) activity.getApplication();
		this.globalData = application.getGlobalData();
		countQuestions = db.getQuestionCount();
		countQuestions.moveToFirst();
		questionCount = countQuestions.getInt(0);
		Log.i(TAG, "questionCount" + questionCount);
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		return inflater.inflate(R.layout.consultingview, container, false);
	}

	@Override
	public void onViewCreated(View view, Bundle savedInstanceState) {
		super.onViewCreated(view, savedInstanceState);
		tvQuestion = (TextView) view.findViewById(R.id.tvQuestion);
		pbProgressQuestions = (ProgressBar) view.findViewById(R.id.pbQuestionProgress);
		rgAnswers = (RadioGroup) view.findViewById(R.id.rgAnswers);
		rbAnswerOne = (RadioButton) view.findViewById(R.id.rbAnswerOne);
		rbAnswerTwo = (RadioButton) view.findViewById(R.id.rbAnswerTwo);
		rbAnswerThree = (RadioButton) view.findViewById(R.id.rbAnswerThree);
		ibPreviousQuestion = (ImageButton) view.findViewById(R.id.ibPreviousQuestion);
		ibNextQuestion = (ImageButton) view.findViewById(R.id.ibNextQuestion);
		btAnalyseQuestions = (Button) view.findViewById(R.id.btAnalyseQuestions);
		btRestartQuestions = (Button) view.findViewById(R.id.btRestartQuestions);
		btRestartQuestions.setOnClickListener(this);
		btAnalyseQuestions.setOnClickListener(this);
		ibPreviousQuestion.setOnClickListener(this);
		ibNextQuestion.setOnClickListener(this);
		intializeView();
	}

	private void intializeView() {
		Log.i(TAG, "currentQuestion: " + currentQuestion);
		if (currentQuestion == questionCount) {
			ibNextQuestion.setVisibility(View.INVISIBLE);
			btAnalyseQuestions.setVisibility(View.VISIBLE);
			btRestartQuestions.setVisibility(View.VISIBLE);

		} else {
			btAnalyseQuestions.setVisibility(View.GONE);
			btRestartQuestions.setVisibility(View.GONE);
			ibPreviousQuestion.setVisibility(View.VISIBLE);
			ibNextQuestion.setVisibility(View.VISIBLE);
		
		}
		if (currentQuestion == 1 || currentQuestion == questionCount) {
			ibPreviousQuestion.setVisibility(View.INVISIBLE);
		} else {
			ibPreviousQuestion.setVisibility(View.VISIBLE);
		}
		
	}

	@Override
	public void onClick(View view) {

		switch (view.getId()) {

		case R.id.ibPreviousQuestion:
			getQuestion(PREVIOUS_QUESTION);
			break;
		case R.id.ibNextQuestion:
			getQuestion(NEXT_QUESTION);
			break;
		case R.id.btAnalyseQuestions: 
			analyseQuestions();
			break;
		case R.id.btRestartQuestions:
			restartQuestions();
			break;
		}

	}
	
	private void getQuestion(int STATE) {
		addConsultingItem();
		
		switch (STATE) {
		
		case NEXT_QUESTION : 
			currentQuestion++; 
			break;
		case PREVIOUS_QUESTION :
			currentQuestion--; break;
		}
		pbProgressQuestions.setProgress(currentQuestion);
		rgAnswers.check(R.id.rbAnswerOne);
		
		//Question
		cursorQuestion = db.getQuestions(currentQuestion);
		while(cursorQuestion.moveToNext()) {
			String question = cursorQuestion.getString(1);
			tvQuestion.setText(question);
		}
	  
		//Answers
		int i = 1;
		cursorAnswers = db.getAnswers(currentQuestion);
		
		if (cursorAnswers.getCount() == 2) {
			rbAnswerThree.setVisibility(View.GONE);
		} else {
			rbAnswerThree.setVisibility(View.VISIBLE);
		}
		
		while(cursorAnswers.moveToNext()) {
			String answer = cursorAnswers.getString(2);
			if (i == 1) {
				rbAnswerOne.setText(answer);
			} else if (i == 2) {
				rbAnswerTwo.setText(answer);
			} else if (i == 3) {
				rbAnswerThree.setText(answer);
			}
			i++;
		
		}
		try {
			int index = globalData.getConsultingItem(currentQuestion-1).getRadioIndex();
			Log.i(TAG, "INDEx:" + index);
			switch (index) {
			case 0 : rgAnswers.check(R.id.rbAnswerOne); break;
			case 1: rgAnswers.check(R.id.rbAnswerTwo); break;
			case 2: rgAnswers.check(R.id.rbAnswerThree); break;
			default: rgAnswers.check(R.id.rbAnswerOne); break; 
			}

		} catch (NullPointerException e) {
			
		}
	
		
		intializeView();
	}
	
	private void addConsultingItem() {
		int location = currentQuestion -1;
		cursorAnswers = db.getAnswers(currentQuestion);
		int answerCount = cursorAnswers.getCount();
		int score = 0;
		int radioIndex = 0;
		String lastQuestion = (String) tvQuestion.getText();
		int selectedID = rgAnswers.getCheckedRadioButtonId();
		RadioButton radioButton = (RadioButton) getActivity().findViewById(selectedID);
		String lastAnswer = (String) radioButton.getText();

		switch (selectedID) {
		case R.id.rbAnswerOne : 
			radioIndex = 0;
			score = 1; break;
		case R.id.rbAnswerTwo : 
			radioIndex = 1;
			if (answerCount == 2) {
				score = 3;
			} else {
				score = 2;
			}
			 break;
		case R.id.rbAnswerThree : 
			radioIndex = 2;
			score = 3; 
			break;
		}
		ConsultingItem consultingItem = new ConsultingItem(lastQuestion, lastAnswer, score, radioIndex);
		
		globalData.addConsultingItem(location, consultingItem);
//		Log.i(TAG, "globaldata:" +globalData + " examplessize"  + globalData.getExamples().size());
		Log.i(TAG, "Question: " + lastQuestion + " Answer" + lastAnswer + " Position: " + currentQuestion + " Size: " + globalData.getConsultingItems().size() + " Score: " + score);

	}

	private void analyseQuestions() {
		addConsultingItem();
		globalData.setConsultingItems(globalData.getConsultingItems());
		ConsultingDialog consultingDialog = ConsultingDialog.newInstance();
		consultingDialog.show(activity.getSupportFragmentManager(), TAG_CONSULTING_DIALOG);
	}
	private void restartQuestions() {
		currentQuestion =  1;
		globalData.deleteConsultingItems();
		getQuestion(NEXT_QUESTION);
		
	}
	

}
